import Router from "vue-router";

const Home = () => import('@/pages/home/home');

const Login = () => import('@/pages/login/Login');

const Human = () => import('@/pages/human/human');

const List = () => import('@/pages/demand/list');

const Edit = () => import('@/pages/demand/edit');

const View = () => import('@/pages/demand/view');

const Register = () => import('@/pages/register/register');

const Handle = () => import('@/pages/demand/handle');

const Index = () => import('@/pages/demand/index');

/** 把组件注册到 路由中去 */
const router = new Router({
    routes: [
        {path : '', redirect : '/login'},
        {path: '/login', component: Login, meta: {layout: 'blank-layout'}},
        {path: '/register', component: Register, meta: {layout: 'blank-layout'}},
        {path : '/home', component : Home, meta: {layout: 'main-layout',requireLogin: true}},
        {path : '/human', component : Human, meta: {layout: 'main-layout',requireLogin: true}},
        {path : '/demand', component : Index, children: [
            {path : '', redirect : 'list'},
            {path : 'list', component : List, meta: {layout: 'main-layout',requireLogin: true}},
            {path : 'company/approve', component : List, meta: {layout: 'main-layout',requireLogin: true}},
            {path : 'dept/approve', component : List, meta: {layout: 'main-layout',requireLogin: true}},
            {path : 'edit', component : Edit, meta: {layout: 'main-layout',requireLogin: true}},
            {path : 'view', component : View, meta: {layout: 'main-layout',requireLogin: true}},
            {path : 'handle', component : Handle, meta: {layout: 'main-layout',requireLogin: true}},
        ]},

        // {path : '/demand/list', component : list},
        // {path : '/demand/company/approve', component : company-approve},
        // {path : '/demand/dept/approve', component : dept-approve},
    ]
});

/** 在路由切换之前的钩子函数：beforeEach */
router.beforeEach((to, from, next) => {
    if(to.meta.requireLogin) {
        let userInfo = router.app.$store.getters['common/getUserInfo']
        if (userInfo && userInfo.token) {
            next()
        }else {
            next('/login')
        }
    }else {
        next();
    }
});

export default router;